<template>
	<view class="index-content">
		<view class="topnav">
			<view :class="item.type==type?'item acitem':'item'" v-for="(item,index) in navList"
				@click="changeNav(item.type)">
				{{$t(item.name)}}
			</view>
		</view>

		<view class="order-list">
			<scroll-view scroll-y="true" style="height:100%;width:100%" @scrolltolower="scrolldata">
				<!-- 我的订单 -->
				<u-empty v-if="orderList.length==0" margin-top="100" mode="order" iconSize="100" textSize="14"
					:text="$t('OrderEmpty')">
				</u-empty>
				<view class="list" v-if="orderList.length" :style="{direction: i18nLocale=='ug'?'rtl':''}"
					v-for="(item,index) in orderList">
					<view class="time">
						<text>{{$t('TimePlaced')}}:</text>
						{{item.paid_at?item.paid_at:item.created_at}}
						<u-icon name="checkmark-circle-fill" color="#494FB6" size="25" v-if="type=='end'"></u-icon>
					</view>
					<view class="order-item">
						<view class="table-content">
							<view class="title">{{$t('TableNumber')}}</view>
							<view class="number">{{item.table_number}}</view>
						</view>
						<view class="item" v-for="(subitem,sub_index) in item.items">
							<view class="left">
								<view class="name">{{i18nLocale=='ug'?subitem.ug_name:subitem.cn_name}}</view>
							</view>
							<view class="right">
								<view class="" style="direction: ltr;" v-if="!item.status">
									<switch :color="colortopic()" @change="changeswitch(item.items,subitem.id,item.id)"
										:checked="subitem.status" :disabled="subitem.status"
										style="transform:scale(0.7)" />
								</view>
								<view class="number">X{{subitem.qty}}</view>
								<view class="price">￥{{subitem.price}}</view>
							</view>
						</view>
						<view class="totalprice-box">
							<text>{{$t('TotalPrice')}}</text>
							<text class="totalprice">￥{{item.fee}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
			<Loading :show="loadshow" iconName="circle" bgZindex="99" bgColor=""  :text="$t('Loading')"></Loading>
			<Loading :show="tabloadshow" bgZindex="99" iconName="circle-round" :showText="false"></Loading>
		</view>
	</view>
</template>

<script>
	import {
		getorders,
		orderstatus,
		itemstatus
	} from '@/api/employee.js';

	export default {
		mounted() {
			this.scrolldata();
		},
		data() {
			return {
				type: 'new',
				navList: [{
						type: 'new',
						name: 'NewOrder'
					},
					{
						type: 'end',
						name: 'Finish'
					}
				],
				orderList: [],
				pageSize: 5,
				page: 1,
				total: 0,
				to: 1,
				tabloadshow: false,
				loadshow: false,

			}
		},
		computed: {
			i18nLocale() {
				return this.$i18n.locale;
			}
		},
		methods: {
			scrolldata() {
				if (this.to != this.total) {
					this.loadshow = true;
					this.getOrders();
				} else {

				}
			},
			changeNav(type) {
				this.type = type;
				this.page = 1;
				this.total = 0;
				this.to = 1;
				this.tabloadshow = true;
				this.orderList = [];
				this.getOrders();
			},
			handdetail(id) {
				uni.navigateTo({
					url: '/pages/order/orderdatail?id=' + id
				})
			},
			getOrders() {
				let that = this;
				getorders({
					per_page: that.pageSize,
					page: that.page,
					status: that.type == 'new' ? 0 : 1,
					shop_id: that.$store.state.employeeinfo.shop_id
				}).then((res) => {
					if (res.code == 200) {
						let data = res.body;
						that.total = data.total;
						that.to = data.to;
						that.page++;
						let mydata = that.orderList.concat(data.data);
						that.orderList = mydata;
						that.tabloadshow = false;
						that.loadshow = false;
					}
				})
			},
			changefinsh(id) {
				let that = this;
				orderstatus({
					id: id,
					status: 1
				}).then((res) => {
					if (res.code == 200) {
						let newarr = that.orderList.filter(item => item.id !== id);
						that.orderList = newarr;
					}
				})
			},
			changeswitch(noitems, id, order_id) {
				let that = this;
				that.orderList.map(bgitem => {
					if (bgitem.id == order_id) {
						bgitem.items.map(item => {
							if (item.id == id) {
								item.status = true;
							}
						})
					}
				})
				itemstatus({
					id: id,
					status: 1
				}).then((res) => {
					if (res.code == 200) {}
				})
				const allTrue = noitems.every(item => Boolean(item.status) == true);
				if (allTrue) {
					this.changefinsh(order_id)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.index-content {
		width: 100%;
		height: 100%;
		box-sizing: border-box;

		.topnav {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-around;
			// background-color: $uni-bg-color-box;
			box-sizing: border-box;
			font-size: $uni-font-size-base;
			margin-top: 10rpx;
			position: relative;
			
			.item {
				width: 40%;
				padding: 10rpx 0rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 30rpx;
			}

			.acitem {
				color: $uni-color-topic;
				background-color: $uni-bg-color-box ;

			}
		}

		.order-list {
			width: 100%;
			height: calc(100% - 40rpx);
			box-sizing: border-box;
			padding: 0rpx 20rpx;

			.list {
				width: 100%;
				background: #FFFFFF;
				box-shadow: $uni-bg-shadow;
				border-radius: $uni-border-radius-lg;
				margin-top: 20rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx 40rpx;
				box-sizing: border-box;

				.time {
					width: 100%;
					height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					font-size: 28rpx;
					color: #000;

					text {
						color: #808080;
					}

					image {
						width: 80rpx;
						height: 80rpx;
					}
				}

				.order-item {
					width: 100%;
					display: flex;
					flex-direction: column;

					.item {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;
						padding: 20rpx 0rpx;

						.left {
							display: flex;
							align-items: center;

							image {
								width: 120rpx;
								height: 120rpx;
								border-radius: 20rpx;
							}

							.name {
								color: $uni-color-title;
								font-size: $uni-font-size-base;
								// padding: 0rpx 30rpx;
							}
						}

						.right {
							display: flex;
							align-items: center;

							.price {
								color: $uni-color-topic;
								font-size: $uni-font-size-base;
								// padding: 0rpx 30rpx;
							}

							.number {
								color: $uni-text-color-grey;
								font-size: $uni-font-size-sm1;
								padding: 0rpx 30rpx;
							}

						}

					}

					.totalprice-box {
						display: flex;
						justify-content: space-between;
						align-items: center;
						font-size: $uni-font-size-base;

						.totalprice {
							color: #f4464b;
							font-size: 32rpx;
						}
					}

					.table-content {
						width: 100%;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.title {
							font-size: $uni-font-size-base;
						}

						.number {
							width: 60rpx;
							height: 60rpx;
							border-radius: 100%;
							display: flex;
							justify-content: center;
							align-items: center;
							background-color: $uni-color-topic;
							color: #fff;
							font-size: $uni-font-size-lg;
						}
					}

					.button {
						background-color: $uni-color-topic;
						border-radius: $uni-border-radius-lg;
						padding: 20rpx 80rpx;
						margin: 0 auto;
						color: #fff;
					}


				}
			}
		}
	}
</style>